package com.dsj.app.controller;

import com.dsj.app.service.DrugInService;
import com.dsj.common.utils.PageWrapper;
import com.dsj.common.utils.Query;
import com.dsj.common.utils.R;
import com.dsj.app.domain.DrugInDO;
import com.dsj.app.domain.DrugInFormDO;
import com.dsj.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 药品入库
 */
@Controller
public class DrugInController extends BaseController {

  @Autowired
  private DrugInService service;

  /**
   * 药品入库页面
   */
  @GetMapping("/inventory/drugin")
  public String drugInPage() {
    return "app/inventory/drug-in/drug-in";
  }

  /**
   * 药品入库登记
   */
  @GetMapping("/inventory/add")
  public String drugInAddPage() {
    return "app/inventory/drug-in/add";
  }

  /**
   * 入库记录列表
   *
   * @param params 查询参数
   */
  @ResponseBody
  @GetMapping("/inventory/list")
  public PageWrapper list(@RequestParam Map<String, Object> params) {
    Query query = new Query(params);
    List<DrugInDO> list = service.list(query);
    return new PageWrapper(list, service.count());
  }

  /**
   * 入库记录保存
   *
   * @param params 参数
   */
  @ResponseBody
  @PostMapping("/inventory/drugin/save")
  public R save(DrugInFormDO params) {
    //    经办人姓名取当前用户的用户名
    String username = getUser().getName();
    params.setManager(username);
    try {
      service.drugInSave(params);
    } catch (Exception e) {
      e.printStackTrace();
      return R.error(1, e.getMessage());
    }
    return R.ok();
  }
}
